﻿using CommonBase;
using CommonClass;
using DAL;
using EnvCommonUtility;
using EnvControlLib;
using InteractiveWhiteboardManagement.Base.ViewModels;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TreeMgtModule.ViewModels
{
    /// <summary>
    /// 课程表
    /// </summary>
    public class ViewModel_SyllabusDetail : WorkspaceViewModel
    {
        #region 变量

        private string _classID;
        private IWMDal _dataOperation = new IWMDal();
        private DelegateCommand<int?> _cmdAddRow, _cmdDeleteRow, _cmdMoveUpRow, _cmdMoveDownRow;

        #endregion

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="infoWindow"></param>
        /// <param name="classID">班级序号</param>
        public ViewModel_SyllabusDetail(Window infoWindow,
            string classID)
        {
            if (string.IsNullOrWhiteSpace(classID))
            {
                MessageBox.Show("未获得班级序号！", ConstantString.MSG_COMPANY);
                return;
            }

            infoWindow.Closing += (s, e) =>
            {
                // To do Sth.
            };

            try
            {
                this._classID = classID;
                SyllabusList = new ObservableCollection<SyllabusModel>(
                    _dataOperation.GetSyllabusItemByClassID("db_VWMSConnectionString",
                    "InteractiveWhiteboardManagement.exe", classID).OrderBy(p => p.Order)); // 获取课程表

                if (SyllabusList.Count == 0)
                {
                    SyllabusList.Add(new SyllabusModel()
                    {
                        ClassID = this._classID,
                        Order = 0,
                    });
                }
                SyllabusSerializeXML = XMLSerializer.SerializeXML(SyllabusList); // 记录最初的课程表信息
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 课程列表
        /// </summary>
        public ObservableCollection<SyllabusModel> SyllabusList { get; set; }

        /// <summary>
        /// 所选列表
        /// </summary>
        public SyllabusModel SelectedSyllabus { get; set; }

        /// <summary>
        /// 记录修改前的课程表序列化字符串，目的：判断课程表属性是否有修改
        /// </summary>
        public string SyllabusSerializeXML { get; private set; }


        /// <summary>
        /// 增加行
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<int?> Cmd_AddRow
        {
            get
            {
                if (_cmdAddRow == null)
                {
                    _cmdAddRow = new DelegateCommand<int?>(_addRow, _canAddRow);
                }
                return _cmdAddRow;
            }
        }

        /// <summary>
        /// 删除行
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<int?> Cmd_DeleteRow
        {
            get
            {
                if (_cmdDeleteRow == null)
                {
                    _cmdDeleteRow = new DelegateCommand<int?>(_deleteRow, _canDeleteRow);
                }
                return _cmdDeleteRow;
            }
        }

        /// <summary>
        /// 上移
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<int?> Cmd_MoveUpRow
        {
            get
            {
                if (_cmdMoveUpRow == null)
                {
                    _cmdMoveUpRow = new DelegateCommand<int?>(_moveUpRow, _canMoveUpRow);
                }
                return _cmdMoveUpRow;
            }
        }

        /// <summary>
        /// 下移
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<int?> Cmd_MoveDownRow
        {
            get
            {
                if (_cmdMoveDownRow == null)
                {
                    _cmdMoveDownRow = new DelegateCommand<int?>(_moveDownRow, _canMoveDownRow);
                }
                return _cmdMoveDownRow;
            }
        }

        #endregion

        #region Private Methods

        #region DelegateCommand

        private bool _canAddRow(int? index)
        {
            return true;
        }

        private bool _canDeleteRow(int? index)
        {
            return true;
        }

        private bool _canMoveUpRow(int? index)
        {
            return true;
        }

        private bool _canMoveDownRow(int? index)
        {
            return true;
        }


        /// <summary>
        /// 增加行
        /// </summary>
        private void _addRow(int? index)
        {
            try
            {
                if (index.HasValue && SyllabusList != null)
                {
                    SyllabusList.Insert(index.Value + 1, new SyllabusModel()
                    {
                        ClassID = this._classID,
                        Order = index.Value + 1,
                    });
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 删除行
        /// </summary>
        private void _deleteRow(int? index)
        {
            try
            {
                if (index.HasValue && SyllabusList != null)
                {
                    ConfirmWindow cw = new ConfirmWindow("你想删除这个条目吗？")
                    {
                        ExtArg=index.Value,
                    };
                    cw.Closed += new EventHandler(_cw_Closed);
                    cw.ShowDialog();
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 上移
        /// </summary>
        private void _moveUpRow(int? index)
        {
            try
            {
                if (index.HasValue && index.Value > 0 &&
                SyllabusList != null && SelectedSyllabus != null)
                {
                    SyllabusModel dummy = new SyllabusModel();
                    Utilities.CopyObject(SelectedSyllabus, dummy); // 深拷贝

                    SyllabusList.RemoveAt(index.Value);
                    SyllabusList.Insert(index.Value - 1, dummy);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 下移
        /// </summary>
        private void _moveDownRow(int? index)
        {
            try
            {
                if (index.HasValue && index.Value < SyllabusList.Count - 1 &&
                SyllabusList != null && SelectedSyllabus != null)
                {
                    SyllabusModel dummy = new SyllabusModel();
                    Utilities.CopyObject(SelectedSyllabus, dummy); // 深拷贝

                    SyllabusList.RemoveAt(index.Value);
                    SyllabusList.Insert(index.Value + 1, dummy);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion

        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _cw_Closed(object sender, EventArgs e)
        {
            if ((sender as ConfirmWindow).DialogResult != true)
                return;

            try
            {
                SyllabusList.RemoveAt(int.Parse((sender as ConfirmWindow).ExtArg.ToString()));

                if (SyllabusList.Count == 0) // 始终保留一行
                {
                    SyllabusList.Add(new SyllabusModel()
                    {
                        ClassID = this._classID,
                        Order = 0,
                    });
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
